package service

import (
	"duoke/tables"
	"duoke/utils/gmd5"
	"duoke/utils/orm"
	"errors"
)

var Staff = new(staffService)

type staffService struct{}

func (s *staffService) Login(username, password string) (*tables.GoodsUser, error) {
	staff := new(tables.GoodsUser)
	has, err := orm.Db.Where("id=?", username).Get(staff)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, errors.New("该账户不存在")
	}
	// 密码校验
	pwd, _ := gmd5.Encrypt(password)
	if staff.Password != pwd {
		return nil, errors.New("密码不正确")
	}
	// 判断当前用户状态
	if staff.IsStaff != 1 {
		return nil, errors.New("您不是员工，无登录权限")
	}
	if err != nil {
		return nil, err
	}
	return staff, nil
}
